/******************************************************************************************************
Copyright (C) 2013 Mestrelab Research S.L. All rights reserved.

This file is part of the Mnova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/
/*globals settings, Dir, MessageBox, CommonSlots, qsTr, File, Process, TextStream, print, qsTr, Python*/
/*jslint plusplus: true, continue: true, bitwise: true*/


function WebUtils() {
	"use strict";
	var pythonVersion = "3";
	this.pythonUtils = new Python(pythonVersion);
	this.isValid = this.pythonUtils.isValid;
}

WebUtils.prototype.request = function (aURL, aAction, aLogin, aPassword) {
	"use strict";
	var sin, response, responseFile, responseFileName, tempDir, xml;

	responseFileName = "response.xml";

	tempDir = new Dir(Dir.temp());
	response = this.executeRequest(aURL, aAction, responseFileName, aLogin, aPassword);

	if (response.exitCode === 0) {
		responseFile = new File(tempDir.filePath(responseFileName));
		responseFile.open(File.ReadOnly);
		sin = new TextStream(responseFile);
		xml = sin.readAll();
		sin.flush();
		responseFile.close();
		File.remove(responseFile.name);
	}
	return {exitCode: response.exitCode, allErrorOutput: response.allErrorOutput, allStrOutput: response.allStrOutput, response: xml};
};

WebUtils.prototype.sendEmail = function (aEmail, aServerInfo, aSecure) {
	"use strict";

	var	scriptFile = "ricares:sendEmail.py",
		parameters = [],
		message;


	parameters.push(aServerInfo.server);
	parameters.push(aServerInfo.login);
	parameters.push(aServerInfo.password);
	parameters.push(aEmail.from);
	parameters.push(JSON.stringify(aEmail.to));
	parameters.push(aEmail.subject);
	parameters.push(aEmail.text);
	parameters.push(JSON.stringify(aEmail.files));
	parameters.push(aEmail.isHtml);


	if (aSecure) {
		parameters.push("True");
	} else {
		parameters.push("False");
	}
	if (aServerInfo.port !== undefined && aServerInfo.port !== "") {
		parameters.push(aServerInfo.port);
	} else {
		parameters.push("False");
	}

	if (aServerInfo.authentication !== undefined) {
		parameters.push(aServerInfo.authentication);
	} else {
		parameters.push(2);
	}

	if (aServerInfo.debug) {
		parameters.push("True");
	} else {
		parameters.push("False");
	}

	message = this.pythonUtils.execute(scriptFile, parameters);
	return message;
};


WebUtils.prototype.soapRequest = function (aURL, aAction, aSearch) {
	"use strict";

	var sin, xml = "",
		soapResponseFileName = "soapResponse.xml",
		responseObject = this.executeSoapRequest(aURL, aAction, aSearch, soapResponseFileName);

	if (responseObject.response.exitCode === 0) {
		responseObject.file.open(File.ReadOnly);
		sin = new TextStream(responseObject.file);
		xml = sin.readAll();
		sin.flush();
	}
	responseObject.file.close();
	File.remove(responseObject.file.name);
	return {exitCode: responseObject.response.exitCode, allErrorOutput: responseObject.response.allErrorOutput, allStrOutput: responseObject.response.allErrorOutput, response: xml};
};

WebUtils.prototype.executeRequest = function (aURL, aAction, aResponseFileName, aLogin, aPassword) {
	"use strict";
	var tempDir = new Dir(Dir.temp()),
		scriptFile = "ricares:request.py",
		responseFile = new File(tempDir.filePath(aResponseFileName)),
		parameters = [],
		message;

	parameters.push(aURL);
	parameters.push(aAction);
	parameters.push(responseFile.name);
	if (aLogin) {
		parameters.push(aLogin);
		parameters.push(aPassword);
	}
	message = this.pythonUtils.execute(scriptFile, parameters);
	return message;

};


WebUtils.prototype.executeSoapRequest = function (aURL, aAction, aSearch, aSoapResponseFileName) {
	"use strict";
	var soapRequestTxt, response,
		parameters = [],
		tempDir = new Dir(Dir.temp()),
		soapRequest = "soapRequest.xml",
		scriptFile = "ricares:soapRequest.py",
		soapResponseFile = new File(tempDir.filePath(aSoapResponseFileName)),
		soapRequestFile = new File(tempDir.filePath(soapRequest));

	soapRequestFile.open(File.WriteOnly);
	soapRequestTxt = new TextStream(soapRequestFile);
	soapRequestTxt.write(aSearch);
	soapRequestTxt.flush();
	soapRequestFile.close();
	parameters.push(aURL);
	parameters.push(aAction);
	parameters.push(soapRequestFile.name);
	parameters.push(soapResponseFile.name);
	response  = this.pythonUtils.execute(scriptFile, parameters);
	File.remove(soapRequestFile.name);
	return {response: response, file: soapResponseFile};
};

